package com.example.finaltest;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.graphics.Bitmap;
import com.example.finaltest.shoppingCart.EmptyShoppingCartActivity;
import android.graphics.BitmapFactory;
import com.example.finaltest.login.UserBean;
import com.example.finaltest.utils.SaveUserUtils;
import com.example.finaltest.utils.URLUtils;

import java.io.File;

import com.example.finaltest.history.HistoryActivity;
import com.example.finaltest.login.LoginActivity;
import com.example.finaltest.login.Person_CenterActivity;
import com.example.finaltest.login.UserBean;
import com.example.finaltest.shoppingCart.Goods;
import com.example.finaltest.shoppingCart.shoppingCartActivity;
import com.example.finaltest.utils.AboutActivity;
import com.example.finaltest.utils.BaseFragment;
import com.example.finaltest.utils.SaveUserUtils;
import com.example.finaltest.utils.SettingActivity;
import com.example.finaltest.utils.URLUtils;
import com.google.gson.Gson;

import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 *我的碎片
 */
public class MeFragment extends BaseFragment {
    TextView logintv;
    Button  shopping,history,usercenter,about,set;
    ImageView login;
    String imgpath;
    private UserBean.DataBean userInfo;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, container, false);
        userInfo = SaveUserUtils.getUserInfo(getContext());
        initView(view);
        return view;
    }


    @Override
    public void onResume() {
        super.onResume();
        setHeadview();
    }

    public void setHeadview() {
        userInfo = SaveUserUtils.getUserInfo(getContext());
        if (userInfo!=null) {
            imgpath = URLUtils.getImgPath(getContext(),userInfo.getPhone());
            File file = new File(imgpath);
            if (file.exists()) {
                Bitmap bitmap = BitmapFactory.decodeFile(imgpath);
                login.setImageBitmap(bitmap);
            }else{
                login.setImageResource(R.mipmap.icon);
            }
        }else{
            login.setImageResource(R.mipmap.icon);
        }
        if (userInfo!=null) {
            String nickname = userInfo.getNickname();
            logintv.setText(nickname);
        }else{
            logintv.setText("请先登录");
        }
    }

    private void setWelcomeText() {
        UserBean.DataBean userInfo = SaveUserUtils.getUserInfo(MeFragment.super.getContext());
        if(userInfo == null){
            logintv.setText("未登录，请先登录！");
        }else {
            String nickname = userInfo.getNickname();
            String msg = nickname+"用户，\n欢迎您！";
            logintv.setText(msg);
        }

    }

    private void initView(View view) {
        login = view.findViewById(R.id.me_picture);
        logintv = view.findViewById(R.id.tx1);
        shopping = view.findViewById(R.id.me_btn_car);
        history = view.findViewById(R.id.me_btn_history);
        usercenter = view.findViewById(R.id.me_btn_pc);
        about = view.findViewById(R.id.me_btn_about);
        set = view.findViewById(R.id.me_btn_set);
        login.setOnClickListener(listener);
        shopping.setOnClickListener(listener);
        logintv.setOnClickListener(listener);
        history.setOnClickListener(listener);
        usercenter.setOnClickListener(listener);
        about.setOnClickListener(listener);
        set.setOnClickListener(listener);
    }
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int id = view.getId();
            switch (id) {
                case  R.id.tx1: case R.id.me_btn_pc: case R.id.me_picture:

                    islogin(0);
                    break;
                case R.id.me_btn_car:
                    int did = islogin1(0);
                    RequestParams params = new RequestParams(URLUtils.queryShoppingCar_url);
                    params.addParameter("userId",did+"");
                    getNetwork1(params);
                    break;
                case R.id.me_btn_history:
                    int did1 = islogin1(0);
                    RequestParams params1 = new RequestParams(URLUtils.querybougth_url);
                    params1.addParameter("userId",did1+"");
                    x.http().get(params1, new CommonCallback<String>() {

                        private String TAG = "mjw123";
                        public void onSuccess(String result) {
                            Log.i(TAG, "onSuccess: result=="+result);
                            Goods goodsBean = new Gson().fromJson(result,Goods.class);
                            int code = goodsBean.getCode();
                            if (code == 200) {
                                Intent intent = new Intent(getContext(), HistoryActivity.class);
                                intent.putExtra("data", goodsBean);
                                startActivity(intent);
                            } else {
                                Toast.makeText(getContext(), "购物历史查询失败", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {

                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {

                        }
                    });
                    break;
                case R.id.me_btn_about:
                    Intent it2 = new Intent(getContext(), AboutActivity.class);
                    //跳转页面
                    it2.putExtra("flag", 0);
                    startActivity(it2);
                    break;
                case R.id.me_btn_set:
                    Intent it1 = new Intent(getContext(), SettingActivity.class);
                    it1.putExtra("flag", 0);
                    startActivity(it1);
                    break;
            }
        }
    };

    private void islogin(int flag) {
        UserBean.DataBean userInfo = SaveUserUtils.getUserInfo(MeFragment.super.getContext());
        Log.i("kkkkk", "islogin: user=="+userInfo);
        if (userInfo == null) {
            /*判断是否登录，如果登录就跳转个人中心页面，如果没有登陆，就跳转登陆页面*/
            Toast.makeText(getContext(),"您还未登录账号,请先登录",Toast.LENGTH_SHORT).show();
            Intent it1 = new Intent(getContext(), LoginActivity.class);
            //跳转页面
            it1.putExtra("flag", flag);
            startActivity(it1);
        } else {
            Intent intent = new Intent(getContext(), Person_CenterActivity.class);
            startActivity(intent);
        }
    }
    private int islogin1(int flag) {
        UserBean.DataBean userInfo = SaveUserUtils.getUserInfo(MeFragment.super.getContext());
        Log.i("kkkkk", "islogin: user=="+userInfo);
        if (userInfo == null) {
            /*判断是否登录，如果登录就跳转个人中心页面，如果没有登陆，就跳转登陆页面*/
            Toast.makeText(getContext(),"您还未登录账号,请先登录",Toast.LENGTH_SHORT).show();
            Intent it1 = new Intent(getContext(), LoginActivity.class);
            //跳转页面
            it1.putExtra("flag", flag);
            startActivity(it1);
            return -1;
        }
        else{
            return userInfo.getId();
        }
    }
    public void onSuccess(String result) {
        super.onSuccess(result);
        Goods goodsBean = new Gson().fromJson(result,Goods.class);
        int code = goodsBean.getCode();
        if (code == 200) {
            Intent intent = new Intent(getContext(), shoppingCartActivity.class);
            intent.putExtra("data", goodsBean);
            startActivity(intent);
        } else {
            Toast.makeText(getContext(), "购物车为空", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(getContext(), EmptyShoppingCartActivity.class);
            startActivity(intent);
        }
    }
}